package com.baichang.taoshunda.dao.impl;

import org.springframework.stereotype.Repository;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.ScoreDao;
import com.baichang.taoshunda.entity.Score;

@Repository
public class ScoreDaoImpl extends BaseDaoImpl<Score> implements ScoreDao {
    /**
     * 获取用户积分
     *
     * @param userId 用户编号
     * @return
     */
    @Override
    public int getScoreByUserId(Integer userId) {
        String sql = "SELECT score FROM `t_score` WHERE `user_id` = ?";
        return queryForInt(sql, userId);
    }

    /**
     * 修改用户积分
     *
     * @param userId 用户编号
     * @param score  积分
     * @return
     */
    @Override
    public int updateScoreByUserId(Integer userId, int score) {
        String sql = "UPDATE `t_score` SET `score`=? WHERE user_id=?";
        return update(sql, score, userId);
    }

    /**
     * 根据用户编号和积分获取类型添加用户积分
     *
     * @param point  积分
     * @param userId 用户编号
     * @return
     */
    @Override
    public boolean addScoreByType(int point, int userId) {
        String sql = "UPDATE `t_score` SET `score`=score+? WHERE user_id=?";
        return update(sql, point, userId) > 0;

    }

    @Override
    public int getScoreSumByUserId(int userId) {
        String sql = "SELECT IFNULL(SUM(score),0) FROM `t_score_list`  WHERE user_id=? AND TO_DAYS(created)=TO_DAYS(NOW()) AND source='分享'";
        return queryForInt(sql, userId);
    }
}